/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.Actions;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Erro;
import model.User;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
//import org.apache.log4j.Logger;

/**
 *
 * @author Adriano
 */


public class DetailsUserAction extends Action {

//    private static final Logger logger = Logger.getLogger(DetailsUserAction.class);
    
    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        
       
        try {
            
           User user = (User)request.getSession().getAttribute("User");
            if(user ==null){
                request.getSession().setAttribute("erroMsg", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");
            }
            
       
            int idUser = Integer.parseInt(request.getParameter("id"));
           
            ArrayList<User> listUser = (ArrayList<User>)request.getSession().getAttribute("ListUser");
            
            User userAtual=null;
            
            for (User u : listUser) {
                if(u.getId() == idUser) {
                    userAtual = u;
                    break;
                }
            }
            
            request.getSession().setAttribute("User", userAtual);
            return mapping.findForward("ResultUserAlias");

        } catch (NumberFormatException e) {
//                logger.error("Error message:", new Exception("DetailsUserAction"));
               
        }
        return null;
    }

}
